﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech.Synthesis;
using System.Threading;
using System.IO;

namespace 语音播报
{
    public partial class Form1 : Form
    {
        private List<string> listWords;
        private int sleepTime;
        private Thread threadRead;
        private int[] orderNum;

        public Form1()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;//方便跨线程调用
            listWords = new List<string>();
            sleepTime = 0;
            orderNum = new int[] { };
        }

        /// <summary>
        /// 暂停按钮响应函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PauseRead(object sender, EventArgs e)
        {
            threadRead.Abort();
        }

        /// <summary>
        /// 开始按钮响应函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRead_Click(object sender, EventArgs e)
        {
            //从文件中获得词语
            string line;
            try
            {
                string filePath = Application.StartupPath+ "\\课文生字.txt"; // @"C:\test\sample.txt"; // specify the path of the text file
                using (StreamReader sr = new StreamReader(filePath))
                {      
                    while ((line = sr.ReadLine()) != null)
                    {
                        listWords.Add(line);
                       //Console.WriteLine(line); // print each line of the text file
                    }
                }
           } 
            catch (Exception )
            {              
            }

           // string s = txtMes.Text.Trim();
            threadRead= new Thread(Read1);
            //设置为后台线程
            threadRead.IsBackground = true;
            //启动线程
            threadRead.Start(listWords);

        }
        private void Read1(object words)
        {
            /*
            Type type = Type.GetTypeFromProgID("SAPI.SpVoice");
            dynamic spVoice = Activator.CreateInstance(type);
            */
            SpeechSynthesizer synthesizer = new SpeechSynthesizer();
            synthesizer.SelectVoiceByHints(VoiceGender.Female, VoiceAge.Adult, 0, new System.Globalization.CultureInfo("zh-Hans"));
            synthesizer.Rate = -2;     
            synthesizer.Volume = 75;


            int[] numbers = new int[listWords.Count];//从文件中读取词语后，需要乱序输出，因此保存其语序
            Random random = new Random();
            for (int i = 0; i < listWords.Count; i++)
            {
                int randomNumber;
                do
                {
                    randomNumber = random.Next(1, listWords.Count + 1);
                } while (Array.IndexOf(numbers, randomNumber) != -1);
                numbers[i] = randomNumber;
            }
            
            for(int i=0;i< listWords.Count; i++)
            {
                int index = numbers[i] - 1; //语序与索引之间差1个数
                synthesizer.Speak(listWords[index]);
                txtMes.Text += listWords[index];
                txtMes.Text += "  ";
                sleepTime = Convert.ToInt32(text_time.Text);
                Thread.Sleep(sleepTime *1000);                
            }
            //第一遍听写结束后，需要两位同学从前往后读一遍
            for(int j=0;j<2;j++)
            {
                for (int i = 0; i < listWords.Count; i++)
                {
                    synthesizer.Speak(listWords[numbers[i] - 1]);
                    Thread.Sleep(2 * 1000);
                }
            }
          
           
        }
        private void Read2(object s)
        {
            SpeechSynthesizer speech = new SpeechSynthesizer();
             
            speech.Rate = 0;
            speech.Volume = 100;
            speech.SpeakAsync(s.ToString());
        }

     
    }
}
